package com.song.quartz.job;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Slf4j
@Component
public class JobHandler {
    @Autowired
    private Scheduler scheduler;

    /**
     * 添加任务
     * @param jobInfo
     * @throws SchedulerException
     * @throws ClassNotFoundException
     */
    public boolean addJob(JobInfo jobInfo){
        Objects.requireNonNull(jobInfo, "");
        //获取 JobKey
        JobKey jobKey = JobKey.jobKey(jobInfo.getJobName(), jobInfo.getJobGroup());
        try {
            //当前任务不存才添加
            if (!scheduler.checkExists(jobKey)){
                Class<Job> jobClass = (Class<Job>) Class.forName(jobInfo.getClassName());
                //任务明细
                JobDetail jobDetail = JobBuilder.newJob(jobClass)
                        .withIdentity(jobKey)
                        .build();
                //配置信息
                jobDetail.getJobDataMap().put("config", jobInfo.getConfig());
                //定义触发器
                TriggerKey triggerKey = TriggerKey.triggerKey(jobInfo.getTriggerName(), jobInfo.getTriggerGroup());
                //定义任务错过机制
                Trigger trigger = TriggerBuilder.newTrigger()
                        .withIdentity(triggerKey)
                        .withSchedule(CronScheduleBuilder.cronSchedule(jobInfo.getCron()).withMisfireHandlingInstructionDoNothing())
                        .build();
                scheduler.scheduleJob(jobDetail, trigger);
                return true;
            }else {
                throw new SchedulerException(jobInfo.getJobName() + "任务已存在，无法重复添加");
            }
        } catch (ClassNotFoundException e) {
            log.error("找不到任务类:\n" + e.getMessage());
        } catch (SchedulerException e) {
            log.error("任务调度异常:\n" + e.getMessage());
        }
        return false;
    }

    /**
     * 暂停任务
     * @param name  任务名称
     * @param group 任务组
     * @throws SchedulerException
     */
    public boolean pauseJob(String name, String group) {
        JobKey jobKey = JobKey.jobKey(name, group);
        try {
            if (scheduler.checkExists(jobKey)){
                scheduler.pauseJob(jobKey);
                return true;
            }
        } catch (SchedulerException e) {
            log.error("任务调度异常:\n" + e.getMessage());
        }
        return false;
    }

    /**
     * 继续任务
     * @param name  任务名称
     * @param group 任务组
     * @throws SchedulerException
     */
    public boolean resumeJob(String name, String group) {
        JobKey jobKey = JobKey.jobKey(name, group);
        try {
            if (scheduler.checkExists(jobKey)){
                scheduler.resumeJob(jobKey);
                return true;
            }
        } catch (SchedulerException e) {
            log.error("任务调度异常:\n" + e.getMessage());
        }
        return false;
    }

    /**
     * 删除任务
     * @param jobInfo
     * @return
     * @throws SchedulerException
     */
    public boolean deleteJob(JobInfo jobInfo) {
        JobKey jobKey = JobKey.jobKey(jobInfo.getJobName(), jobInfo.getJobGroup());
        try {
            if (scheduler.checkExists(jobKey)){
                TriggerKey triggerKey = TriggerKey.triggerKey(jobInfo.getTriggerName(), jobInfo.getTriggerGroup());
                scheduler.pauseTrigger(triggerKey);
                scheduler.unscheduleJob(triggerKey);
                return scheduler.deleteJob(jobKey);
            }
        } catch (SchedulerException e) {
            log.error("任务调度异常:\n" + e.getMessage());
        }
        return false;
    }

}
